<?php
require_once 'AuthController.php';
class Customer extends CI_Controller {
	
	private $avatar;
	
	function Customer() {
		parent::__construct ();
			$this->load->model ( 'be/customer_model', 'model' );
			$this->load->library ( 'form_validation' );
			$this->load->helper ( array ('form', 'url' ) );
	}
	
	function index() {
		$this->list_all_customers ();
	}
	
	function add() {
		$auth = new AuthController();
		if($auth->can('add', 'customer')) {
			
			$this->form_validation->set_rules ( 'fullname', 'Họ tên','required');
			$this->form_validation->set_rules ( 'username', 'Tên tài khoản', 'required|min_lenght[5]|max_lenght[20]|is_unique[customers.username]');
			$this->form_validation->set_rules ( 'password', 'Mật khẩu', 'required|matches[password]' );
			$this->form_validation->set_rules ( 'email', 'Email', 'required|valid_email|is_unique[customers.email]' );
			$this->form_validation->set_rules ( 'phone', 'Điện thoại','required' );
			$this->form_validation->set_rules ( 'address', 'Địa chỉ' , 'required' );
			$this->form_validation->set_rules ( 'district', 'Quận/Huyện', 'required' );
			$this->form_validation->set_rules ( 'province', 'Tỉnh/Thành phố', 'required' );
			$this->form_validation->set_rules ( 'active_status', 'Trạng thái', 'required' );
			$this->form_validation->set_rules ( 'avatar', 'Hình ảnh', 'callback_get_avatar_uploaded' );
			$this->form_validation->set_rules ( 'group', 'Nhóm khách hàng', 'required' );
			$this->form_validation->set_rules ( 'description', 'Mô tả', 'required' );
			
			if ($this->form_validation->run () == FALSE) {
				$data ['page_title'] = 'Thêm mới khách hàng';
				$data ['main_content'] = 'be/customer/add';
				$this->load->view ( 'be/includes/template', $data );
			} else {
				$data ['name'] = $this->input->post ( 'fullname' );
				$data ['username'] = $this->input->post ( 'username' );
				$data ['password'] = $this->input->post ( 'password' );
				$data ['email'] = $this->input->post ( 'email' );
				$data ['phone'] = $this->input->post ( 'phone' );
				$data ['address'] = $this->input->post ( 'address' );
				$data ['district'] = $this->input->post ( 'district' );
				$data ['province'] = $this->input->post ( 'province' );
				$data ['active_status'] = $this->input->post ( 'active_status' );
				$data ['group'] = $this->input->post ( 'group' );
				$data ['description'] = $this->input->post ( 'description' );
				
				$data ['avatar'] = $this->avatar;
				
				if (! $this->model->add ( $data )) {
					$data ['page_title'] = 'Có lỗi xảy ra';
					$mess_detail = '<p>Hãy kiểm tra lại thông tin bạn đã điền vào form!</p>';
					$mess_detail .= '<p><a href="' . base_url () . 'index.php/customer/add"> Làm lại</a> hoặc <a href="' . base_url () . 'index.php/customer">Trở lại trang danh sách</a></p>';
					$data ['message_detail'] = $mess_detail;
				} else {
					$data ['page_title'] = 'Thành công';
					$mess_detail = '<p>Thêm mới khách hàng thành công!</p>';
					$mess_detail .= '<p><a href="' . base_url () . 'index.php/customer/add">Tiếp tục</a> hoặc <a href="' . base_url () . 'index.php/customer">Trở lại trang danh sách</a>';
					$data ['message_detail'] = $mess_detail;
				}
				$data ['main_content'] = 'be/customer/message';
				$this->load->view ( 'be/includes/template', $data );
			}
		}
		else {
			$auth->denied();
		}
	}
	
	function get_avatar_uploaded() {
		//Create location if it does not exist
		$location = DEFAULT_IMAGE_UPLOAD_PATH . '/customer';
		if (! is_dir ( $location )) {
			if (! mkdir ( $location, 0755 )) {
				die ( 'Failed to create folders...' );
			}
		}
		$config ['upload_path'] = $location;
		$config ['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
		$config ['max_size'] = '10000';
		
		$this->load->library ( 'upload', $config );
		if ($this->upload->do_upload ( 'avatar' )) {
			$tmp = $this->upload->data ( 'avatar' );
			$this->avatar = $location . '/' . $tmp ['file_name'];
		} else {
			$this->avatar =  CUSTOMER_DEFAULT_AVATAR;
		}
	}
	

	function get_by_fillter(){
		/*if($_POST['name']==''&& $_POST['province']=='0' && $_POST['district']=='0' && $_POST['status']=='0' && $_POST['group']=='0', $_POST['phone']=='' && $_POST['username']==''){
			redirect(site_url('customer'));
		}
		else{*/
			$_POST['name'].
			$data ['customers'] = $this->model->get_by_fillter(trim($_POST['name']), $_POST['province'], $_POST['district'], $_POST['status'], $_POST['group'], trim($_POST['phone']), trim($_POST['username']));
			$data ['province'] = $this->model->get_all_province();
			$data ['district'] = $this->model->get_all_district();
			$data ['page_title'] = 'Danh sách khách hàng';
			$data ['main_content'] = 'be/customer/list';
			$this->load->view ( 'be/includes/template', $data );
		//}
	}
		
	function list_all_customers() {
		$auth = new AuthController();
		if($auth->can('view', 'customer')) {
			
			$data ['province'] = $this->model->get_all_province();
			$data ['district'] = $this->model->get_all_district();
			$data ['customers'] = $this->model->get ( );
			
			$data ['page_title'] = 'Danh sách khách hàng';
			$data ['main_content'] = 'be/customer/list';
			$this->load->view ( 'be/includes/template', $data );
		}else {
			$auth->denied();
		}
	}
	
	function searchByProvince() {
		$province = $this->input->post('province');
		$data ['customers'] = $this->model->searchByProvince($province);
		
		$data ['page_title'] = 'Danh sách khách hàng';
		$data ['main_content'] = 'be/customer/list';
		$this->load->view ( 'be/includes/template', $data );
	}
	function view($id){
		$auth = new AuthController();
		if($auth->can('view', 'customer')) {
			$customer = $this->model->get($id);
					
			$data['id'] = $id;
			$data ['name'] = $customer->name;
			$data ['username'] = $customer->username;
			$data ['password'] = $customer->password;
			$data ['email'] = $customer->email;
			$data ['phone'] = $customer->phone;
			$data ['address'] = $customer->address;
			$data ['district'] = $customer->district;
			$data ['province'] = $customer->province;
			$data ['active_status'] = $customer->active_status;
			$data ['avatar'] = $customer->avatar;
			$data ['group'] = $customer->group;
			$data ['description'] = $customer->description;
			$data ['page_title'] = 'Thông tin khách hàng';
			$data ['main_content'] = 'be/customer/detail';
			$this->load->view ( 'be/includes/template', $data );
		}else {
			$auth->denied();
		}
	}
	
	function update($id) {		
		$auth = new AuthController();
		if($auth->can('update', 'customer')) {
			$customer = $this->model->get($id);
					
			$data['id'] = $id;
			$data ['name'] = $customer->name;
			$data ['username'] = $customer->username;
			$data ['password'] = $customer->password;
			$data ['email'] = $customer->email;
			$data ['phone'] = $customer->phone;
			$data ['address'] = $customer->address;
			$data ['district'] = $customer->district;
			$data ['province'] = $customer->province;
			$data ['active_status'] = $customer->active_status;
			$data ['avatar'] = $customer->avatar;
			$data ['group'] = $customer->group;
			$data ['description'] = $customer->description;
			
			
			$this->form_validation->set_rules ( 'fullname', 'Họ tên' );
			$this->form_validation->set_rules ( 'username', 'Tên tài khoản' );
			$this->form_validation->set_rules ( 'password', 'Mật khẩu' );
			$this->form_validation->set_rules ( 'email', 'Email' );
			$this->form_validation->set_rules ( 'phone', 'Điện thoại' );
			$this->form_validation->set_rules ( 'address', 'Địa chỉ' );
			$this->form_validation->set_rules ( 'district', 'Quận/Huyện' );
			$this->form_validation->set_rules ( 'province', 'Tỉnh/Thành phố' );
			$this->form_validation->set_rules ( 'active_status', 'Trạng thái' );
			$this->form_validation->set_rules ( 'avatar', 'Hình ảnh', 'callback_get_avatar_uploaded' );
			$this->form_validation->set_rules ( 'group', 'Nhóm khách hàng' );
			$this->form_validation->set_rules ( 'description', 'Mô tả' );
			
			if ($this->form_validation->run () == FALSE) {
				$data ['page_title'] = 'Cập nhật thông tin khách hàng';
				$data ['main_content'] = 'be/customer/update';
				$this->load->view ( 'be/includes/template', $data );
			} else {
				$data ['name'] = $this->input->post ( 'fullname' );
				$data ['username'] = $this->input->post ( 'username' );
				$data ['password'] = $this->input->post ( 'password' );
				$data ['email'] = $this->input->post ( 'email' );
				$data ['phone'] = $this->input->post ( 'phone' );
				$data ['address'] = $this->input->post ( 'address' );
				$data ['district'] = $this->input->post ( 'district' );
				$data ['province'] = $this->input->post ( 'province' );
				$data ['active_status'] = $this->input->post ( 'active_status' );
				$data ['group'] = $this->input->post ( 'group' );
				$data ['description'] = $this->input->post ( 'description' );
				
				if($this->avatar == CUSTOMER_DEFAULT_AVATAR ) {
					$data ['avatar'] = $customer->avatar;
				}else {
					$data['avatar'] = $this->avatar;
				}					
				if (! $this->model->update ($id, $data )) {
					$data ['page_title'] = 'Có lỗi xảy ra';
					$mess_detail = '<p>Hãy kiểm tra lại thông tin bạn đã điền vào form!</p>';
					$mess_detail .= '<p><a href="' . base_url () . 'index.php/customer/update"> Làm lại</a> hoặc <a href="' . base_url () . 'index.php/customer">Trở lại trang danh sách</a></p>';
					$data ['message_detail'] = $mess_detail;
				} else {
					$data ['page_title'] = 'Thành công';
					$mess_detail = '<p>Cập nhật thành công!</p>';
					$mess_detail .= '<p><a href="' . base_url () . 'index.php/customer/update">Tiếp tục</a> hoặc <a href="' . base_url () . 'index.php/customer">Trở lại trang danh sách</a>';
					$data ['message_detail'] = $mess_detail;
				}
				$data ['main_content'] = 'be/customer/message';
				$this->load->view ( 'be/includes/template', $data );
			}
		}else {
			$auth->denied();
		}
	}
	
	function delete() {
		$auth = new AuthController();
		if($auth->can('delete', 'customer')) {
			$ids = $this->input->post('selected');
			foreach ($ids as $id){
				$this->model->delete($id);
			}
			redirect(site_url('customer'));
		}else {
			$auth->denied();
		}
	}
}